package config

import (
	"fmt"
	"github.com/spf13/viper"
)

type Configuration struct {
	Database DatabaseConfig
	Server   ServerConfig
	Redis    RedisConfig
	Logger   LoggerConfig
}

type DatabaseConfig struct {
	DbType             string `yaml:"DbType"`
	DbHost             string `yaml:"DbHost"`
	DbPort             string `yaml:"DbPort"`
	DbUser             string `yaml:"DbUser"`
	DbPassword         string `yaml:"DbPassword"`
	DbName             string `yaml:"DbName"`
	MaxIdleConnections int    `yaml:"MaxIdleConnections"`
	MaxOpenConnections int    `yaml:"MaxOpenConnections"`
}
type RedisConfig struct {
	RdbType     string `yaml:"RdbType"`
	RdbUser     string `yaml:"RdbUser"`
	RdbHost     string `yaml:"RdbHost"`
	RdbPort     string `yaml:"RdbPort"`
	RdbPassword string `yaml:"RdbPassword"`
	Rdb         string `yaml:"Rdb"`
}
type ServerConfig struct {
	HttpPort string `yaml:"HttpPort"`
	Mode     string `yaml:"Mode"`
}
type LoggerConfig struct {
	Level   string `yaml:"Level"`
	LogType string `yaml:"LogType"`
}

func InitConfig(cfg *Configuration) {
	viper.SetConfigName("config")
	viper.SetConfigType("yaml")
	viper.AddConfigPath("./internal/config")

	err := viper.ReadInConfig()
	if err != nil {
		panic(fmt.Errorf("Config file not found or invalid: %s", err))
	}

	err = viper.Unmarshal(&cfg)
	if err != nil {
		panic(err)
	}

	//fmt.Println("mode", viper.GetString("Server.Mode"))
	//viper.Set("Server.Mode", "release")
	//fmt.Println("mode", viper.GetString("Server.Mode"))
}
